//
//  QSWorkStationSearchView.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/5/20.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSWorkStationSearchView.h"
@interface QSWorkStationSearchView ()<UITextFieldDelegate>

@end
@implementation QSWorkStationSearchView
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self configSubView];
        self.backgroundColor = [UIColor QSColorWhiteFFFFFF];    
    }
    return self;
}

- (void)configSubView{
    
    [self addSubview:self.whiteBackGroundView];
    [self.whiteBackGroundView addSubview:self.leftLabel];
    [self.whiteBackGroundView addSubview:self.rightLabel];
    [self.whiteBackGroundView addSubview:self.searchTextField];
    [_whiteBackGroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self);
    }];
    [_leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.whiteBackGroundView).offset(kRealValue(12));
        make.top.equalTo (self.whiteBackGroundView).offset(kRealValue(11));
    }];
    [_rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.whiteBackGroundView).offset(- kRealValue(12));
        make.centerY.equalTo(self.leftLabel);
    }];
    [_searchTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.leftLabel.mas_bottom).offset(kRealValue(15));
        make.left.equalTo(self.whiteBackGroundView).offset(kRealValue(10));
        make.right.equalTo(self.whiteBackGroundView).offset(- kRealValue(10));
        make.height.offset(kRealValue(30));
    }];
    
}

#pragma mark - Lazy load
- (UIView *)whiteBackGroundView{
    if (!_whiteBackGroundView) {
        _whiteBackGroundView = [[UIView alloc]init];
        _whiteBackGroundView.backgroundColor = [UIColor QSColorWhiteFFFFFF];
    }
    return _whiteBackGroundView;
}

- (UILabel *)leftLabel{
    if (!_leftLabel) {
        _leftLabel = [UILabel labelWithName:@"完成情况" font:[UIFont QSFontOfSize15] textColor:[UIColor QSColorBlack434343] textAlignment:NSTextAlignmentLeft];
    }
    return _leftLabel;
}

- (UILabel *)rightLabel{
    if (!_rightLabel) {
        _rightLabel = [UILabel labelWithName:@"已完成：" font:[UIFont QSFontOfSize14] textColor:[UIColor QSColorGray999999] textAlignment:NSTextAlignmentRight];
    }
    return _rightLabel;
}

- (UITextField *)searchTextField{
    if (!_searchTextField) {
        _searchTextField = [[UITextField alloc]init];
        _searchTextField.backgroundColor = [UIColor colorWithHexString:@"#F2F4F8"];
        _searchTextField.layer.cornerRadius = 15;
        UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(10, 9, 12, 12)];
        imageview.contentMode = UIViewContentModeCenter;
        imageview.image = [UIImage imageNamed:@"search"];
        _searchTextField.leftView = imageview;
        _searchTextField.leftViewMode = UITextFieldViewModeAlways;
        _searchTextField.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@"请输入名称进行搜索" attributes:@{NSFontAttributeName:[UIFont QSFontOfSize12]}];
        _searchTextField.font = [UIFont QSFontOfSize12];
        _searchTextField.delegate = self;
//        _searchTextField.placeholder = @"请输入名称进行搜索";
        
    }
    return _searchTextField;
}

//收起键盘开始搜索
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (self.delegate && [self.delegate respondsToSelector:@selector(headerSearchViewDidSearchAction:)]) {
        [self.delegate headerSearchViewDidSearchAction:self.searchTextField.text];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //禁止输入空格
    NSString *tem = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]componentsJoinedByString:@""];
    if (![string isEqualToString:tem]) {
        return NO;
    }
    return YES;
}
//换行变搜索
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"7");// 点击键盘的‘换行’会调用
    [textField resignFirstResponder];
    return YES;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
